package com.hltx.service.user.impl;

import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.hltx.constants.RedisConstant;
import com.hltx.pojo.user.User;
import com.hltx.service.user_dubbo.UserRedisUtil;
import com.hltx.util.CustomUtil;
import com.hltx.util.ShortUrlUtil;

import redis.clients.jedis.JedisCluster;

/**
 * 
 * @author admin
 * @version 2017.06.09 10:00
 * 
 */
@Service (protocol = "dubbo", loadbalance = "roundrobin")
public class UserRedisService implements UserRedisUtil {

   
    @Autowired
    private JedisCluster jedisCluster;

	@Override
	public String makeUserToken(int userId, String key) {
		String nonce = ShortUrlUtil.buildCode(8);
		String timestamp = Long.toString(System.currentTimeMillis()); 
		Map<String, String> params = new TreeMap<String, String>();
		params.put("userId", Integer.toString(userId));
		params.put("nonce", nonce);
		params.put("timestamp", timestamp);
		String userToken = CustomUtil.createSign(params, key);
		return userToken;
	}

	@Override
	public String getUserToken(String key) {
		return jedisCluster.get(key);
	}

	@Override
	public void setUserToken(String key, String value) {
		jedisCluster.set(key, value);
		jedisCluster.expire(key, 1800);
	}


	@Override
	public void setUserTokenExpire(String key) {
		jedisCluster.expire(key, 1800);
	}

	@Override
	public void setUserToken(User result, String userToken) {
		String value = result.getId()+"_"+result.getLoginId();
		setUserToken(RedisConstant.USER_TOOKEN+userToken,value);
	}

	@Override
	public void removeUserToken(String key) {
		jedisCluster.del(key);
	}

	@Override
	public boolean isExists(String key) {
		return jedisCluster.exists(key);
	} 
    
    
    
}
